﻿using System;
using System.Threading;
using System.Windows;
using Appology.SugarSync.IO;
using Appology.SugarSync.IO.Primitives;
using Microsoft.Phone.Controls;
using System.Threading.Tasks;

namespace Appology.SugarSync.Explorer.Phone
{
    public partial class CreateFolder : PhoneApplicationPage
    {
        public CreateFolder()
        {
            InitializeComponent();

            DataContext = ApplicationState.Current.History.Pop();

            Loaded += CreateFolder_Loaded;
        }

        void CreateFolder_Loaded(object sender, RoutedEventArgs e)
        {
            FolderName.Focus();
        }

        private async void Save_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(FolderName.Text))
            {
                MessageBox.Show("Folder name is required.");
                return;
            }

            var directoryInfo = (SugarSyncDirectoryInfo)DataContext;

            ApplicationState.Current.History.Push((SugarSyncPathInfo)DataContext);

            await directoryInfo.CreateDirectoryInfo(FolderName.Text);

            await TaskEx.Delay(TimeSpan.FromSeconds(1));

            NavigationService.GoBack();
        }
    }
}